$(document).ready(function(){

    base_url_srch = 'http://localhost/asp_timetable/';
    //base_url_srch = 'http://www.busterminal.me/admin/';

    $("#srch_polazak").focus();

    $('#srch_dolazak').blur(function() {

        $("#trazi").focus();

    });

    // TABLSE SORTER

    $("#polasci_tbl").tablesorter({

        headers: { 
            0: { sorter: false }, 
            1: { sorter: false }, 
            2: { sorter: false }, 
            3: { sorter: false }, 
            4: { sorter: false }, 
            5: { sorter: false } 
        },

        widthFixed: false,

        widgets: ['zebra']

    });  

    $('.stanice_tip').live('click', function(){

        $("#tooltip").remove();

        $("body").append("<div id='tooltip'></div>");

        $('#tooltip').css('width','330px');

        var id = this.id;

        $.ajax({

            url : base_url_srch+'polazak/core/pogledaj_stop_stanice',

            type: 'post',

            data: {
                id_polaska:id,
                type: timetable_type
            },

            dataType:'json',

            success: function(data){

                $("#tooltip").html(data.html);

                $("#stop_stanice_tbl").tablesorter({

                    headers: {
                        0: { sorter: false }, 1: { sorter: false }, 2: { sorter: false }, 3: { sorter: false }, 4: { sorter: false }   
                    },

                    widthFixed: false,

                    widgets: ['zebra']

                })

            }
        });

        $("#tooltip")

        .css("top", ($("#polasci_tbl").offset().top) + "px")

        .css("left", ($("#polasci_tbl").offset().left) + "px")

        .fadeIn(100);

    })

    $('#close_stop').live('click',function(){

        $("#tooltip").remove();

    })

    $('.polazak_tip').live('click', function(){

        $("#tooltip").remove();

        $("body").append("<div id='tooltip'></div>");

        $('#tooltip').css('width','230px');

        var id = this.id.split("-");

        $.ajax({

            url : base_url_srch+'polazak/core/pogledaj_detalje_polaska',

            type: 'post',

            data: {
                id_polaska:id[0],
                tip_polaska: id[1]
            },

            dataType:'json',

            success: function(data){

                $("#tooltip").html(data.html);

                $("#stop_stanice_tbl").tablesorter({

                    headers: {
                        0: { sorter: false }, 1: { sorter: false }, 2: { sorter: false }, 3: { sorter: false }, 4: { sorter: false }   
                    },

                    widthFixed: false,

                    widgets: ['zebra']

                })

            }
        });

        $("#tooltip")

        .css("top", ($("#polasci_tbl").offset().top) + "px")

        .css("left", ($("#polasci_tbl").offset().left) + "px")

        .fadeIn(100);

    })       

    $("#prevoznik_pgr a").live('click', function(evt) {               

        evt.preventDefault();

        var that = jQuery(this).busy({ img :base_url_srch+'assets/img/loader/ajax-loader-red.gif'});
        
        /***********************************************************
        * DATEPICKER 1
        ***********************************************************/
        var chosen_date = $("#datumkomponenta1").datepicker("getDate");        
        
        var mesec = chosen_date.getMonth()+1;
        var godina = chosen_date.getFullYear();
                            
        seldate = chosen_date.getDate() + "-" + mesec + "-" + godina;

        $.ajax({
            type: 'GET',
            url:  $(this).get()+'/?jsoncall=?',
            data: ({ 
                srch_polazak : $('#srch_polazak').val(),
                srch_dolazak : $('#srch_dolazak').val(),
                datum: seldate,
                type: timetable_type  
            }),
            dataType: 'jsonp',
            success: function(data){

                that.busy('hide');
                $('#infomessage_search').hide();
                $('#polasci_tbl tbody').html(data.html);
                $('#paginator').html(data.paginator);

            },
            error:function(data){
                $.modal.close(); 
                alert("Error: " + data);
            }
        });

    });            

    /*$("#srch_polazak").val('PODGORICA');
    $("#srch_dolazak").val('BREMEN'); */


    /***********************************************************
    * Datum komponenta
    ***********************************************************/
    date = new Date();

    // get the current date
    var minDate = new Date(date.getFullYear(), date.getMonth(), 1); //one day next before month

    $('#datumkomponenta1').html('');
    $('#datumkomponenta1').datepicker( "destroy" );

    $.datepicker.setDefaults($.datepicker.regional['sr']);

    $('#datumkomponenta1').datepicker({
        inline: false,
        hideIfNoPrevNext: true,
        minDate: minDate,
        dateFormat: 'dd.mm.yy',
        numberOfMonths: 1,
        showOn: 'button',
        buttonImage: base_url+'assets/img/calendar_icon1.png',
        buttonImageOnly: true,
        onSelect: function(dateStr) {
            var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);
        } 
    });

    $('#datumkomponenta1').datepicker('setDate', new Date(date.getFullYear(), date.getMonth(), date.getDate()));       


});